package com.hongdee.crm.taskConfig.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.taskConfig.entity.TCrmCarTaskConf;
import com.hongdee.crm.taskConfig.service.ITCrmCarTaskConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by My on 2017/5/16.
 */
@RestController
@RequestMapping("/crm/car/task/conf")
public class TCrmCarTaskConfController {


    @Autowired
    private ITCrmCarTaskConfService taskConfService;

    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "code", defaultValue = "")String code,
                    @RequestParam(name = "start", defaultValue = "")String start,
                    @RequestParam(name = "limit", defaultValue = "")String limit) throws Exception {
        String pcode=this.getPcode(code);
        Map map=taskConfService.page(pcode,code,uid,start,limit);
        map.put("success",true);
        return map;
    }

    private String getPcode(String code){
        Map<String,String> map=new HashMap<>();
        map.put("cnj","bx");
        map.put("cns","bx");
        map.put("xbj","bx");
        map.put("xbs","bx");
        map.put("hyt","bx");
        map.put("fhyt","bx");
        map.put("jdj","bx");
        map.put("jds","bx");
        map.put("sr","ga");
        map.put("ns","ga");
        map.put("zbt","ga");
        map.put("ybt","ga");
        map.put("6","ls");
        map.put("7","ls");
        map.put("8","ls");
        map.put("9","ls");
        map.put("10","ls");
        map.put("11","ls");
        map.put("12","ls");
        map.put("sb_mbr","by");
        map.put("sb_mby","by");
        map.put("eb_mbr","by");
        map.put("eb_mby","by");
        map.put("db_mbr","by");
        map.put("db_mby","by");
        map.put("sy_mbr","by");
        map.put("zdy","qt");
        return map.get(code);
    }

    /**
     * 保存车辆分配规则
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarTaskConf data) throws Exception {
        ResultEntity res=new ResultEntity();
        taskConfService.saveTaskConf(data,uid);
        res.setSuccess(true);
        res.setMsg("任务分配规则保存成功!!");
        return res;
    }


    /**
     * 保存车辆分配规则
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/kx/save")
    @ResponseBody
    public ResultEntity kxSave(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarTaskConf data) throws Exception {
        ResultEntity res=new ResultEntity();
        taskConfService.saveTaskConf(data,uid,"kx");
        res.setSuccess(true);
        res.setMsg("任务分配规则保存成功!!");
        return res;
    }

    /**
     * 根据车辆分配规则，进行车辆分配
     * @param uid
     * @param token
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/rest")
    @ResponseBody
    public ResultEntity rest(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res=new ResultEntity();
        taskConfService.distCarByCarConf(id,uid,null);
        res.setSuccess(true);
        res.setMsg("任务资源分配完成！！");
        return  res;
    }

    @RequestMapping("/restVin")
    @ResponseBody
    public ResultEntity restVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id,
                                @RequestParam(name = "upExecl", defaultValue = "upExecl") MultipartFile file
    ) throws Exception {
        ResultEntity res=new ResultEntity();
        taskConfService.distCarByCarConf(id,uid,file);
        res.setSuccess(true);
        res.setMsg("任务资源分配完成！！");
        return  res;
    }

    /**
     * 删除任务资源分配信息
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/del")
    @ResponseBody
    public ResultEntity del(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id)throws Exception{
        ResultEntity res=new ResultEntity();
        taskConfService.delTaskConf(id,uid);
        res.setSuccess(true);
        res.setMsg("任务资源清除完成！！");
        return res;
    }



}
